<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>C/C++ 中 static 的用法全局变量与局部变量 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../w3cnote/cpp-static-usage.html" />
<meta name="keywords" content="C/C++ 中 static 的用法全局变量与局部变量">
<meta name="description" content="1. 什么是static? static 是 C/C++ 中很常用的修饰符，它被用来控制变量的存储方式和可见性。  1.1 static 的引入  我们知道在函数内部定义的变量，当程序执行到它的定义处时，编译器为它在栈上分配空间，函数在栈上分配的空间在此函数执行结束时会释放掉，这样就产生了一个问题: 如果想将函数中此变量的值保存至下一次调用时，如何实现？ 最容易想到的方法是定义为全局的变量，但定义一个全局变量有许多缺点，最明显的缺点是破..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<div class="container navigation">
    <div class="row">
        <div class="col nav">
            

                        <ul class="pc-nav" id="note-nav">
                <li><a href="../">首页</a></li>
                <li><a href="../w3cnote">笔记首页</a></li>
                <li><a href="../w3cnote/android-tutorial-intro.html" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" title="ES6 教程">ES6 教程</a></li>
                <li><a href="../w3cnote/ten-sorting-algorithm.html" title="排序算法">排序算法</a></li>
                <li><a href="../w3cnote/hadoop-tutorial.html" title="Hadoop 教程">Hadoop</a></li>
                <li><a href="../w3cnote/zookeeper-tutorial.html" title="Zookeeper 教程">Zookeeper</a></li>
                <li><a href="../w3cnote/verilog-tutorial.html" title="Verilog 教程">Verilog</a></li>
                <li><a href="../w3cnote_genre/code" title="编程技术">编程技术</a></li> 
                <li><a href="../w3cnote_genre/coderlife" title="程序员人生">程序员人生</a></li>
                
                <!--<li><a href="javascript:;" class="runoob-pop">登录</a></li>
                
                
                        <li>
                <a style="font-weight:bold;" href="../linux/linux-tutorial.html#yunserver" target="_blank" onclick="_hmt.push(['_trackEvent', 'aliyun', 'click', 'aliyun'])" title="kkb">云服务器</a>
                </li>
                <li><a href="http://gk.link/a/104mQ" target="_blank" style="font-weight: bold;"onclick="_hmt.push(['_trackEvent', '极客时间', 'click', 'jike'])" title="我的圈子">极客时间</a></li>
            
                
                <li><a target="_blank" href="../shoppinglist" rel="nofollow">知识店铺</a></li> 
        -->
            </ul>
                        
              
            <ul class="mobile-nav">
                <li><a href="../w3cnote">首页</a></li>
                <li><a href="../w3cnote_genre/android" target="_blank" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" target="_blank" title="ES6 教程">ES6</a></li>
                <li><a href="../w3cnote_genre/joke" target="_blank" title="程序员笑话">逗乐</a></li>
                
                <a href="javascript:void(0)" class="search-reveal">Search</a> 
            </ul>
            
        </div>
    </div>
</div>


<!--  内容  -->
<div class="container main">
	<div class="row">

		<div class="col middle-column big-middle-column">
	 			<div class="article">
			<div class="article-heading">
				<h2>C/C++ 中 static 的用法全局变量与局部变量</h2>				<h3><em>分类</em> <a href="../w3cnote_genre/code" title="编程技术" >编程技术</a> </h3>
			</div>
			<div class="article-body note-body">
				<div class="article-intro">
					
<h2>1. 什么是static?</h2>
<p>static 是 C/C++ 中很常用的修饰符，它被用来控制变量的存储方式和可见性。</p>
<h3>
1.1 static 的引入</h3>

<p>我们知道在函数内部定义的变量，当程序执行到它的定义处时，编译器为它在栈上分配空间，函数在栈上分配的空间在此函数执行结束时会释放掉，这样就产生了一个问题: 如果想将函数中此变量的值保存至下一次调用时，如何实现？ 最容易想到的方法是定义为全局的变量，但定义一个全局变量有许多缺点，最明显的缺点是破坏了此变量的访问范围（使得在此函数中定义的变量，不仅仅只受此函数控制）。static 关键字则可以很好的解决这个问题。</p>

<p>另外，在 C++ 中，需要一个数据对象为整个类而非某个对象服务,同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部，对外不可见时，可将其定义为静态数据。</p>
<h3>1.2 静态数据的存储</h3>

<p><strong>全局（静态）存储区</strong>：分为 DATA 段和 BSS 段。DATA 段（全局初始化区）存放初始化的全局变量和静态变量；BSS 段（全局未初始化区）存放未初始化的全局变量和静态变量。程序运行结束时自动释放。其中BBS段在程序执行之前会被系统自动清0，所以未初始化的全局变量和静态变量在程序执行之前已经为0。存储在静态数据区的变量会在程序刚开始运行时就完成初始化，也是唯一的一次初始化。</p>

<p>在 C++ 中 static 的内部实现机制：静态数据成员要在程序一开始运行时就必须存在。因为函数在程序运行中被调用，所以静态数据成员不能在任何函数内分配空间和初始化。</p>
<p>这样，它的空间分配有三个可能的地方，一是作为类的外部接口的头文件，那里有类声明；二是类定义的内部实现，那里有类的成员函数定义；三是应用程序的 main() 函数前的全局数据声明和定义处。</p>
<p>静态数据成员要实际地分配空间，故不能在类的声明中定义（只能声明数据成员）。类声明只声明一个类的"尺寸和规格"，并不进行实际的内存分配，所以在类声明中写成定义是错误的。它也不能在头文件中类声明的外部定义，因为那会造成在多个使用该类的源文件中，对其重复定义。</p>
<p>static 被引入以告知编译器，将变量存储在程序的静态存储区而非栈上空间，静态数据成员按定义出现的先后顺序依次初始化，注意静态成员嵌套时，要保证所嵌套的成员已经初始化了。消除时的顺序是初始化的反顺序。</p>
<p><strong>优势：</strong>可以节省内存，因为它是所有对象所公有的，因此，对多个对象来说，静态数据成员只存储一处，供所有对象共用。静态数据成员的值对每个对象都是一样，但它的值是可以更新的。只要对静态数据成员的值更新一次，保证所有对象存取更新后的相同的值，这样可以提高时间效率。</p>

<hr><h2>
2. 在 C/C++ 中static的作用</h2>
<h3>2.1 总的来说</h3>
<ul><li>
（1）在修饰变量的时候，static 修饰的静态局部变量只执行初始化一次，而且延长了局部变量的生命周期，直到程序运行结束以后才释放。</li><li>
（2）static 修饰全局变量的时候，这个全局变量只能在本文件中访问，不能在其它文件中访问，即便是 extern 外部声明也不可以。</li><li>
（3）static 修饰一个函数，则这个函数的只能在本文件中调用，不能被其他文件调用。static 修饰的变量存放在全局数据区的静态变量区，包括全局静态变量和局部静态变量，都在全局数据区分配内存。初始化的时候自动初始化为 0。</li><li>
（4）不想被释放的时候，可以使用static修饰。比如修饰函数中存放在栈空间的数组。如果不想让这个数组在函数调用结束释放可以使用 static 修饰。</li><li>
（5）考虑到数据安全性（当程序想要使用全局变量的时候应该先考虑使用 static）。</li></ul>
<h3>
2.2 静态变量与普通变量</h3>
<p><strong>静态全局变量有以下特点：</strong></p>
<ul><li>
（1）静态变量都在全局数据区分配内存，包括后面将要提到的静态局部变量;</li><li>
（2）未经初始化的静态全局变量会被程序自动初始化为0（在函数体内声明的自动变量的值是随机的，除非它被显式初始化，而在函数体外被声明的自动变量也会被初始化为 0）；</li><li>
（3）静态全局变量在声明它的整个文件都是可见的，而在文件之外是不可见的。</li></ul>
<p><strong>优点：</strong>静态全局变量不能被其它文件所用；其它文件中可以定义相同名字的变量，不会发生冲突。</p>
<p><strong>（1）全局变量和全局静态变量的区别</strong></p>
<ul>
<li>
1）全局变量是不显式用 static 修饰的全局变量，全局变量默认是有外部链接性的，作用域是整个工程，在一个文件内定义的全局变量，在另一个文件中，通过 extern 全局变量名的声明，就可以使用全局变量。</li><li>
2）全局静态变量是显式用 static 修饰的全局变量，作用域是声明此变量所在的文件，其他的文件即使用 extern 声明也不能使用。</li></ul>
<h3>2.3 静态局部变量有以下特点：</h3><ul><li>
（1）该变量在全局数据区分配内存；</li><li>
（2）静态局部变量在程序执行到该对象的声明处时被首次初始化，即以后的函数调用不再进行初始化；</li><li>
（3）静态局部变量一般在声明处初始化，如果没有显式初始化，会被程序自动初始化为 0；</li><li>
（4）它始终驻留在全局数据区，直到程序运行结束。但其作用域为局部作用域，当定义它的函数或语句块结束时，其作用域随之结束。</li></ul>
<p>一般程序把新产生的动态数据存放在堆区，函数内部的自动变量存放在栈区。自动变量一般会随着函数的退出而释放空间，静态数据（即使是函数内部的静态局部变量）也存放在全局数据区。全局数据区的数据并不会因为函数的退出而释放空间。</p>
<p>看下面的例子：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">//example:</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">stdio.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro">  </span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">stdlib.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro">  </span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">k1</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">k2</span><span class="hl-code">;
</span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">k3</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;
</span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">k4</span><span class="hl-code">;
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">m1</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-identifier">m2</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
    </span><span class="hl-types">char</span><span class="hl-code">*</span><span class="hl-identifier">p</span><span class="hl-code">;
    </span><span class="hl-types">char</span><span class="hl-code"> </span><span class="hl-identifier">str</span><span class="hl-brackets">[</span><span class="hl-number">10</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">hello</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-types">char</span><span class="hl-code">*</span><span class="hl-identifier">q</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">hello</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-identifier">p</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-brackets">)</span><span class="hl-identifier">malloc</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">free</span><span class="hl-brackets">(</span><span class="hl-identifier">p</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">栈区-变量地址    i：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">i</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">栈区-变量地址   p：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">p</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">栈区-变量地址 str：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">str</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">栈区-变量地址   q：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">q</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">堆区地址-动态申请：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">p</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">全局外部有初值 k1：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">k1</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">   外部无初值 k2：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">k2</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">静态外部有初值 k3：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">k3</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">   外静无初值 k4：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">k4</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">  内静态有初值 m1：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">m1</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">  内静态无初值 m2：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">m2</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">    文字常量地址：%p, %s</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">q</span><span class="hl-code">, </span><span class="hl-identifier">q</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">      程序区地址：%p</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, &amp;</span><span class="hl-identifier">main</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div></div>
<p>输出结果如下：</p>
<p><img decoding="async" src="/wp-content/uploads/2018/11/1069650-20170723092636356-1932751621.png"></p>
<hr><h2>
3. static 用法</h2>
<h3>3.1 在 C++ 中</h3>
<p>static 关键字最基本的用法是：</p>
<ul><li>
1、被 static 修饰的变量属于类变量，可以通过<span class="marked">类名.变量名</span>直接引用，而不需要 new 出一个类来
</li><li>
2、被 static 修饰的方法属于类方法，可以通过<span class="marked">类名.方法名</span>直接引用，而不需要 new 出一个类来
</li></ul>
<p>被 static 修饰的变量、被 static 修饰的方法统一属于类的静态资源，是类实例之间共享的，换言之，一处变、处处变。</p>

<p>在 C++ 中，静态成员是属于整个类的而不是某个对象，静态成员变量只存储一份供所有对象共用。所以在所有对象中都可以共享它。使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则，保证了安全性还可以节省内存。</p>

<p>静态成员的定义或声明要加个关键 static。静态成员可以通过双冒号来使用即 <span class="marked">&lt;类名&gt;::&lt;静态成员名&gt;</span>。</p>

<h3>3.2 静态类相关</h3>

通过类名调用静态成员函数和非静态成员函数:
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Point</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
</span><span class="hl-reserved">public</span><span class="hl-code">:   
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">    
    </span><span class="hl-brackets">}</span><span class="hl-code">  
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
</span><span class="hl-brackets">}</span><span class="hl-code">;  
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-identifier">Point</span><span class="hl-code">::</span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
    </span><span class="hl-identifier">Point</span><span class="hl-code">::</span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span></div>
</div></div>
<p><img decoding="async" src="/wp-content/uploads/2018/11/1069650-20170722200004746-352704528.png"></p>
<p>报错：</p> <pre>'Point::init' : illegal call of non-static member function</pre>

<p><strong>结论 1：</strong>不能通过类名来调用类的非静态成员函数。</p>

<p>通过类的对象调用静态成员函数和非静态成员函数。</p><div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Point</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
</span><span class="hl-reserved">public</span><span class="hl-code">:   
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">    
    </span><span class="hl-brackets">}</span><span class="hl-code">  
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
</span><span class="hl-brackets">}</span><span class="hl-code">; 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-identifier">Point</span><span class="hl-code"> </span><span class="hl-identifier">pt</span><span class="hl-code">;  
    </span><span class="hl-identifier">pt</span><span class="hl-code">.</span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
    </span><span class="hl-identifier">pt</span><span class="hl-code">.</span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span></div>
</div></div>

<p>编译通过。</p>

<p><strong>结论 2</strong>：类的对象可以使用静态成员函数和非静态成员函数。</p>

<p>在类的静态成员函数中使用类的非静态成员。</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">stdio.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro">  </span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Point</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
</span><span class="hl-reserved">public</span><span class="hl-code">:   
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">    
    </span><span class="hl-brackets">}</span><span class="hl-code">  
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">  
        </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">%d</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">m_x</span><span class="hl-brackets">)</span><span class="hl-code">;  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
</span><span class="hl-reserved">private</span><span class="hl-code">:  
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">m_x</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span><span class="hl-code">;  
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-identifier">Point</span><span class="hl-code"> </span><span class="hl-identifier">pt</span><span class="hl-code">;  
    </span><span class="hl-identifier">pt</span><span class="hl-code">.</span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span></div>
</div></div>

<p>编译出错：</p><pre>error C2597: illegal reference to data member 'Point::m_x' in a static member function</pre>

<p>因为静态成员函数属于整个类，在类实例化对象之前就已经分配空间了，而类的非静态成员必须在类实例化对象后才有内存空间，所以这个调用就出错了，就好比没有声明一个变量却提前使用它一样。</p>

<p><strong>结论3：</strong>静态成员函数中不能引用非静态成员。</p>

<p>在类的非静态成员函数中使用类的静态成员。</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Point</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
</span><span class="hl-reserved">public</span><span class="hl-code">:   
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">    
        </span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
</span><span class="hl-brackets">}</span><span class="hl-code">;  
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-identifier">Point</span><span class="hl-code"> </span><span class="hl-identifier">pt</span><span class="hl-code">;  
    </span><span class="hl-identifier">Pt</span><span class="hl-code">.</span><span class="hl-identifier">init</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">pt</span><span class="hl-code">.</span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span></div>
</div></div>
<p>编译通过。</p>

<p><strong>结论 4</strong>：类的非静态成员函数可以调用用静态成员函数，但反之不能。</p>

<p>使用类的静态成员变量。</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">stdio.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro">  </span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Point</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
</span><span class="hl-reserved">public</span><span class="hl-code">:   
    </span><span class="hl-identifier">Point</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">    
        </span><span class="hl-identifier">m_nPointCount</span><span class="hl-code">++;  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
    ~</span><span class="hl-identifier">Point</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">  
        </span><span class="hl-identifier">m_nPointCount</span><span class="hl-code">--;  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
    </span><span class="hl-brackets">{</span><span class="hl-code">  
        </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">%d</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">m_nPointCount</span><span class="hl-brackets">)</span><span class="hl-code">;  
    </span><span class="hl-brackets">}</span><span class="hl-code">  
</span><span class="hl-reserved">private</span><span class="hl-code">:  
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">m_nPointCount</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span><span class="hl-code">;  
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-identifier">Point</span><span class="hl-code"> </span><span class="hl-identifier">pt</span><span class="hl-code">;  
    </span><span class="hl-identifier">pt</span><span class="hl-code">.</span><span class="hl-identifier">output</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span></div>
</div></div>

<p>按 <strong>Ctrl+F7</strong> 编译无错误，按 F7 生成 EXE 程序时报链接错误。</p>

<pre>error LNK2001: unresolved external symbol "private: static int Point::m_nPointCount" (?m_nPointCount@Point@@0HA)</pre>

<p>这是因为类的静态成员变量在使用前必须先初始化。</p>

<p>在 <strong>main()</strong> 函数前加上 <span class="marked">int Point::m_nPointCount = 0;</span> 再编译链接无错误，运行程序将输出 1。</p>

<p><strong>结论 5</strong>：类的静态成员变量必须先初始化再使用。</p>

<p><strong>思考总结：</strong>静态资源属于类，但是是独立于类存在的。从 J 类的加载机制的角度讲，静态资源是类初始化的时候加载的，而非静态资源是类实例化对象的时候加载的。 类的初始化早于类实例化对象，比如 <span class="marked">Class.forName("xxx")</span> 方法，就是初始化了一个类，但是并没有实例化对象，只是加载这个类的静态资源罢 了。所以对于静态资源来说，它是不可能知道一个类中有哪些非静态资源的；但是对于非静态资源来说就不一样了，由于它是实例化对象出来之后产生的，因此属于类的这些东西它都能认识。所以上面的几个问题答案就很明确了：</p>
<ul><li>
1）静态方法能不能引用非静态资源？不能，实例化对象的时候才会产生的东西，对于初始化后就存在的静态资源来说，根本不认识它。
</li><li>
2）静态方法里面能不能引用静态资源？可以，因为都是类初始化的时候加载的，大家相互都认识。
</li><li>
3）非静态方法里面能不能引用静态资源？可以，非静态方法就是实例方法，那是实例化对象之后才产生的，那么属于类的内容它都认识。</li></ul>

<p>（<strong>static 修饰类：</strong>这个用得相对比前面的用法少多了，static 一般情况下来说是不可以修饰类的， 如果 static 要修饰一个类，说明这个类是一个静态内部类（注意 static 只能修饰一个内部类），也就是匿名内部类。像线程池 ThreadPoolExecutor 中的四种拒绝机制 CallerRunsPolicy、AbortPolicy、DiscardPolicy、 DiscardOldestPolicy 就是静态内部类。静态内部类相关内容会在写内部类的时候专门讲到。）</p>
<h3>
3.3 总结：</h3><ul><li>
（1）静态成员函数中不能调用非静态成员。
</li><li>
（2）非静态成员函数中可以调用静态成员。因为静态成员属于类本身，在类的对象产生之前就已经存在了，所以在非静态成员函数中是可以调用静态成员的。
</li><li>
（3）静态成员变量使用前必须先初始化(如 <span class="marked">int MyClass::m_nNumber = 0;</span>)，否则会在 linker 时出错。
</li></ul>


<p><strong>一般总结</strong>：在类中，static 可以用来修饰静态数据成员和静态成员方法。 </p>
<p><strong>静态数据成员 </strong></p>
<ul><li>
（1）静态数据成员可以实现多个对象之间的数据共享，它是类的所有对象的共享成员，它在内存中只占一份空间，如果改变它的值，则各对象中这个数据成员的值都被改变。 </li><li>
（2）静态数据成员是在程序开始运行时被分配空间，到程序结束之后才释放，只要类中指定了静态数据成员，即使不定义对象，也会为静态数据成员分配空间。 </li><li>
（3）静态数据成员可以被初始化，但是只能在类体外进行初始化，若未对静态数据成员赋初值，则编译器会自动为其初始化为 0。 </li><li>
（4）静态数据成员既可以通过对象名引用，也可以通过类名引用。</li></ul>

<p><strong>静态成员函数 </strong></p>
<ul><li>
（1）静态成员函数和静态数据成员一样，他们都属于类的静态成员，而不是对象成员。</li><li> 
（2）非静态成员函数有 this 指针，而静态成员函数没有 this 指针。 </li><li>
（3）静态成员函数主要用来方位静态数据成员而不能访问非静态成员。</li></ul>


<p>再给一个利用类的静态成员变量和函数的例子以加深理解，这个例子建立一个学生类，每个学生类的对象将组成一个双向链表，用一个静态成员变量记录这个双向链表的表头，一个静态成员函数输出这个双向链表。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">stdio.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">string.h</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">MAX_NAME_SIZE</span><span class="hl-code"> = </span><span class="hl-number">30</span><span class="hl-code">;  
 
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Student</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">  
</span><span class="hl-reserved">public</span><span class="hl-code">:  
    </span><span class="hl-identifier">Student</span><span class="hl-brackets">(</span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">pszName</span><span class="hl-brackets">)</span><span class="hl-code">;
    ~</span><span class="hl-identifier">Student</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">PrintfAllStudents</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">private</span><span class="hl-code">:  
    </span><span class="hl-types">char</span><span class="hl-code">    </span><span class="hl-identifier">m_name</span><span class="hl-brackets">[</span><span class="hl-identifier">MAX_NAME_SIZE</span><span class="hl-brackets">]</span><span class="hl-code">;  
    </span><span class="hl-identifier">Student</span><span class="hl-code"> *</span><span class="hl-identifier">next</span><span class="hl-code">;
    </span><span class="hl-identifier">Student</span><span class="hl-code"> *</span><span class="hl-identifier">prev</span><span class="hl-code">;
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-identifier">Student</span><span class="hl-code"> *</span><span class="hl-identifier">m_head</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;  
 
</span><span class="hl-identifier">Student</span><span class="hl-code">::</span><span class="hl-identifier">Student</span><span class="hl-brackets">(</span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">pszName</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-identifier">strcpy</span><span class="hl-brackets">(</span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">m_name</span><span class="hl-code">, </span><span class="hl-identifier">pszName</span><span class="hl-brackets">)</span><span class="hl-code">;
 
    </span><span class="hl-comment">//建立双向链表，新数据从链表头部插入。</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">next</span><span class="hl-code"> = </span><span class="hl-identifier">m_head</span><span class="hl-code">;
    </span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">prev</span><span class="hl-code"> = </span><span class="hl-prepro">NULL</span><span class="hl-code">;
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">m_head</span><span class="hl-code"> != </span><span class="hl-prepro">NULL</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">m_head</span><span class="hl-code">-&gt;</span><span class="hl-identifier">prev</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">;
    </span><span class="hl-identifier">m_head</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">;  
</span><span class="hl-brackets">}</span><span class="hl-code">  
 
</span><span class="hl-identifier">Student</span><span class="hl-code">::~</span><span class="hl-identifier">Student</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-comment">//析构过程就是节点的脱离过程  </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">  
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">this</span><span class="hl-code"> == </span><span class="hl-identifier">m_head</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-comment">//该节点就是头节点。</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">m_head</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">next</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code">
    </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">prev</span><span class="hl-code">-&gt;</span><span class="hl-identifier">next</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">next</span><span class="hl-code">;
        </span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">next</span><span class="hl-code">-&gt;</span><span class="hl-identifier">prev</span><span class="hl-code"> = </span><span class="hl-reserved">this</span><span class="hl-code">-&gt;</span><span class="hl-identifier">prev</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">  
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">Student</span><span class="hl-code">::</span><span class="hl-identifier">PrintfAllStudents</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Student</span><span class="hl-code"> *</span><span class="hl-identifier">p</span><span class="hl-code"> = </span><span class="hl-identifier">m_head</span><span class="hl-code">; </span><span class="hl-identifier">p</span><span class="hl-code"> != </span><span class="hl-prepro">NULL</span><span class="hl-code">; </span><span class="hl-identifier">p</span><span class="hl-code"> = </span><span class="hl-identifier">p</span><span class="hl-code">-&gt;</span><span class="hl-identifier">next</span><span class="hl-brackets">)</span><span class="hl-code">
        </span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">%s</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">p</span><span class="hl-code">-&gt;</span><span class="hl-identifier">m_name</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-identifier">Student</span><span class="hl-code">* </span><span class="hl-identifier">Student</span><span class="hl-code">::</span><span class="hl-identifier">m_head</span><span class="hl-code"> = </span><span class="hl-prepro">NULL</span><span class="hl-code">;  
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">  
</span><span class="hl-brackets">{</span><span class="hl-code">   
    </span><span class="hl-identifier">Student</span><span class="hl-code"> </span><span class="hl-identifier">studentA</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">AAA</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Student</span><span class="hl-code"> </span><span class="hl-identifier">studentB</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">BBB</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Student</span><span class="hl-code"> </span><span class="hl-identifier">studentC</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">CCC</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Student</span><span class="hl-code"> </span><span class="hl-identifier">studentD</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">DDD</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Student</span><span class="hl-code"> </span><span class="hl-identifier">student</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">MoreWindows</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Student</span><span class="hl-code">::</span><span class="hl-identifier">PrintfAllStudents</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div></div>
<p>程序将输出：</p>
<p><img decoding="async" src="/wp-content/uploads/2018/11/0_1314353805XGo7.jpg"></p>
<blockquote><p>原文地址：https://www.cnblogs.com/33debug/p/7223869.html</p></blockquote>

				</div>
			</div>
			<div class="previous-next-links">
			<div class="previous-design-link">← <a href="../w3cnote/java-capture-group.html" rel="prev"> Java 正则表达式的捕获组(capture group)</a> </div>
			<div class="next-design-link"><a href="../w3cnote/redis-external-network.html" rel="next"> 配置 redis 外网可访问</a> →</div>
			</div>
			<style>
.wrapper {
  /*text-transform: uppercase; */
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  white-space: nowrap;
  font-size: 14px;
  text-align: left;
  background: #96b97d;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 14px;
  pointer-events: none;
  position: absolute;
  
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
	color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #96b97d 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 20%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}
.wrapper .tooltip1 {
	margin-left: 50px;
	padding-top: 0px;
}
/*
.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}
*/
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

</style>

<link rel="stylesheet" href="../0/upvotejs.css">
<script src=""></script>
<script src=""></script>
<div id="respond" class="no_webshot"> 
		<div class="comment-signarea" style=" padding: 20px 20px;"> 
	<h3 class="text-muted" id="share_code" style="color: #799961;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 点我分享笔记</h3>
	<!--
	<p style="font-size:14px;">笔记需要是本篇文章的内容扩展！</p><br>
	<p style="font-size:12px;"><a href="../tougao" target="_blank">文章投稿，可点击这里</a></p>
	<p style="font-size:14px;"><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>
		<h3 class="text-muted"><i class="fa fa-info-circle" aria-hidden="true"></i> 分享笔记前必须<a href="javascript:;" class="runoob-pop">登录</a>！</h3>
		<p><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>-->
	</div>
		
	<form action="/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;">
		<div class="comt">
			<div class="comt-title">
				<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i>				<p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
			</div>
			<div class="comt-box">
			<div id="mded"></div>
			
				<div class="comt-ctrl">
					<div class="comt-tips"><input type='hidden' name='comment_post_ID' value='21053' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</div>
					<button type="submit" name="submit" id="submit" tabindex="5"><i class="fa fa-pencil" aria-hidden="true"></i> 分享笔记</button>
				</div>
			</div>
		
				
					<div class="comt-comterinfo"> 
						<ul id="comment-author-info">
							<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称"><span class="text-muted">昵称 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱"><span class="text-muted">邮箱 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址"><span class="text-muted">引用地址</span></li>
						</ul>
					</div>
				
			
		</div>

	</form>
	</div>
<script type="text/javascript">
$(function() {
	//初始化编辑器
	
	var editor = new Simditor({
	  textarea: $('#mded'),
	  placeholder: '写笔记...',
	  upload:false,
	 // upload: {url:'/api/comment_upload_file.php',params: null,fileKey: 'upload_file',connectionCount: 1,leaveConfirm: '文件正在上传，您确定离开?'},
	  defaultImage: 'https://www.runoob.com/images/logo.png',
	  codeLanguages: '',
	  autosave: 'editor-content',
	  toolbar: [  'bold','code','ul','ol','image' ]
	});
	editor.on('selectionchanged', function() {
		$(".code-popover").hide();
	});

	// 提交数据
	$("#share_code").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		
	});
	$("#user_add_note").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		$('html, body').animate({
       	    scrollTop: $("#respond").offset().top
    	}, 200);
	});

	// 提交笔记
	var commentform=$('#commentform');
	commentform.prepend('<div id="comment-status" style="display:none;" ></div>');
	var statusdiv=$('#comment-status');
	
	commentform.submit(function(e){
		e.preventDefault();
		var noteContent = editor.getValue();
		// console.log(noteContent);
		noteContent = noteContent.replace(/<pre><code>/g,"<pre>");
		noteContent = noteContent.replace(/<\/code><\/pre>/g,"</pre>");
		
		// 系列化表单数据
		var comment_parent = 0;
		var is_user_logged_in = $("#is_user_logged_in").val();
		var comment_post_ID =  21053;
		var _wp_unfiltered_html_comment = $("#_wp_unfiltered_html_comment").val();
		var comment = noteContent;
		var author = $("#author").val();
		var url = $("#url").val();
		var email = $("#email").val();
		if(isBlank(author) && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入昵称！</p>').show();
		} else if(isBlank(email)  && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入邮箱！</p>').show();
		} else {
			// var formdata=commentform.serialize() + "&comment=" + noteContent ;
			// 添加状态信息
			statusdiv.html('<p>Processing...</p>').show();
			// 获取表单提交地址
			var formurl=commentform.attr('action');
			
			// 异步提交
			$.ajax({
					type: 'post',
					url: formurl,
					dataType:'json',
					data: {"comment_parent":comment_parent,"comment_post_ID":comment_post_ID, "_wp_unfiltered_html_comment":_wp_unfiltered_html_comment,"comment":comment,"url":url, "email":email,"author":author},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					statusdiv.html('<p class="ajax-error" >数据不完整或表单提交太快了！</p>').show();
				},
				success: function(data, textStatus){
					if(data.errorno=="0") {
						$("#submit").prop('disabled', true);
						statusdiv.html('<p class="ajax-success" >笔记已提交审核，感谢分享笔记！</p>').show();
						alert('笔记已提交审核，感谢分享笔记！');
					}else{
						statusdiv.html('<p class="ajax-error" >'+data.msg+'</p>').show();
					}
					commentform.find('textarea[name=comment]').val('');
				}
			});
			setTimeout(function(){
		        $("#submit").prop('disabled', false);
		    }, 10*1000);
		}
		return false;

	});
	$(".comt-author").click(function() {
		href = $(this).children("a").attr("href");
		if(href.indexOf("/note/")!=-1) {
			var win = window.open(href, '_blank');
  			win.focus();
		}
	});
	$(".comt-meta span").hover(function(){
		$(this).children(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).children(".tooltip").removeAttr("style");
	});
	/*
	$(".wrapper i").hover(function(){
		$(this).siblings(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).siblings(".tooltip").css({ "opacity": 0, "pointer-events": "auto"});
	});
	*/
	//Upvote.create('runoobvote-id', {callback: vote_callback});
	var ajaxurl = 'https://www.runoob.com/wp-admin/admin-ajax.php';
	var callback = function(data) {
		//console.log($('#runoobvote-id').upvote('upvoted'));
		//console.log($('#runoobvote-id').upvote('downvoted'));
		//console.log(data);
		_vote_action = data.action;
		id_arr = data.id.split('-');
		um_id= id_arr[2];
		//console.log(um_id);
		
		var re = /^[1-9]+/;
		if (re.test(um_id)) { 
			var ajax_data = {
				_vote_action: _vote_action,
				action: "pinglun_zan",
				um_id: um_id,
				um_action: "ding"
			};
			//console.log(ajax_data);
			$.post(ajaxurl,ajax_data,function(status){
				//if(status.vote_num>999) {
				//	_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(status.vote_num) +'</span>';
				//	$("#runoobvote-id-" + um_id + " .count").hide().after(_voteHtml);
				//}
				
			});
		}
	};
	if($('#comments').length && $('.upvotejs').length){
		$('.upvotejs').upvote({id: 21053, callback: callback});
	
		$.post(ajaxurl,{"action":"pinglun_zan","postid":21053},function(data){  
			$(data).each(function(key,value) {
				$("#runoobvote-id-" + value.commid + " .upvote").addClass(value.upvotejs_class);
				$("#runoobvote-id-" + value.commid + " .downvote").addClass(value.downvote_class);
				$("#runoobvote-id-" + value.commid + " .count").text(value.upvote_count);
			})
		},'json');
		
	}
	
	
});
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
function kFormatter(num) {
	// return num;
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

</script>

<link rel="stylesheet" href="../0/qa.css?1.44">
<link rel="stylesheet" type="text/css" href="../0/simditor.min.css" />
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
			<div class="article-heading-ad" id="w3cnote-ad728">
			<script async src=""></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="horizontal"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
			</div>
			<style>
@media screen and (max-width: 768px) {
	#w3cnote-ad728 {
		display: none;
	}
}
p.note-author {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    font-weight: bold;
    color: #78a15a;
    padding-bottom: 2px;
    margin-bottom: 4px;
}
</style>
<script>
var aid = 21053;
</script>
	</div>
		
	</div>
	<div class="listcol last right-column">




<!--
	<div class="tab tab-light-blue"> 订阅</div>
	<div class="sidebar-box">
		<div class="socialicons">
			<a href="../feed" class="rss">RSS 订阅</a>
		
			<form action="//list.qq.com/cgi-bin/qf_compose_send" method="post">
			<input type="hidden" value="qf_booked_feedback" name="t">
			<input type="hidden" value="4b67b6b6c1f5e792559940cab4aebb8f1126fba880bff1a8" name="id">
			<input class="placeholder" id="feed_email" name="to" value="输入邮箱 订阅笔记" autocomplete="off">
			<input type="submit" value="订阅" class="btn btn-primary">
			</form>
		
		</div>
 
	</div>
-->	



<!--
	<div class="sidebar-box cate-list">
	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">笔记列表</a>
		</div>

 

</div>
-->

	 <div class="sidebar-box cate-list">
		 		

	 	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">教程列表</a>
		</div>
		
		<div class="cate-items"> 
				<a href="../ado">ADO 教程</a>
	<a href="../ajax">Ajax 教程</a>
	<a href="../android">Android 教程</a>
	<a href="../angularjs2">Angular2 教程</a>
	<a href="../angularjs">AngularJS 教程</a>
	<a href="../appml">AppML 教程</a>
	<a href="../asp">ASP 教程</a>
	<a href="../aspnet">ASP.NET 教程</a>
	<a href="../bootstrap">Bootstrap 教程</a>
	<a href="../bootstrap4">Bootstrap4 教程</a>
	<a href="../bootstrap5">Bootstrap5 教程</a>
	<a href="../cprogramming">C 教程</a>
	<a href="../csharp">C# 教程</a>
	<a href="../cplusplus">C++ 教程</a>
	<a href="../chartjs">Chart.js 教程</a>
	<a href="../cssref">CSS 参考手册</a>
	<a href="../css">CSS 教程</a>
	<a href="../css3">CSS3 教程</a>
	<a href="../django">Django 教程</a>
	<a href="../docker">Docker 教程</a>
	<a href="../dtd">DTD 教程</a>
	<a href="../echarts">ECharts 教程</a>
	<a href="../eclipse">Eclipse 教程</a>
	<a href="../firebug">Firebug 教程</a>
	<a href="../font-awesome">Font Awesome 图标</a>
	<a href="../foundation">Foundation 教程</a>
	<a href="../git">Git 教程</a>
	<a href="../go">Go 语言教程</a>
	<a href="../googleapi">Google 地图 API 教程</a>
	<a href="../highcharts">Highcharts 教程</a>
	<a href="../htmldom">HTML DOM 教程</a>
	<a href="../tags">HTML 参考手册</a>
	<a href="../charsets">HTML 字符集</a>
	<a href="../html">HTML 教程</a>
	<a href="../http">HTTP 教程</a>
	<a href="../ionic">ionic 教程</a>
	<a href="../ios">iOS 教程</a>
	<a href="../java">Java 教程</a>
	<a href="../jsref">JavaScript 参考手册</a>
	<a href="../js">Javascript 教程</a>
	<a href="../jeasyui">jQuery EasyUI 教程</a>
	<a href="../jquerymobile">jQuery Mobile 教程</a>
	<a href="../jqueryui">jQuery UI 教程</a>
	<a href="../jquery">jQuery 教程</a>
	<a href="../json">JSON 教程</a>
	<a href="../jsp">JSP 教程</a>
	<a href="../julia">Julia 教程</a>
	<a href="../kotlin">Kotlin 教程</a>
	<a href="../linux">Linux 教程</a>
	<a href="../lua">Lua 教程</a>
	<a href="../markdown">Markdown 教程</a>
	<a href="../matplotlib">Matplotlib 教程</a>
	<a href="../maven">Maven 教程</a>
	<a href="../memcached">Memcached 教程</a>
	<a href="../mongodb">MongoDB 教程</a>
	<a href="../mysql">MySQL 教程</a>
	<a href="../nodejs">Node.js 教程</a>
	<a href="../numpy">NumPy 教程</a>
	<a href="../pandas">Pandas 教程</a>
	<a href="../perl">Perl 教程</a>
	<a href="../php">PHP 教程</a>
	<a href="../postgresql">PostgreSQL 教程</a>
	<a href="../python3">Python 3 教程</a>
	<a href="../python">Python 基础教程</a>
	<a href="../r">R 教程</a>
	<a href="../rdf">RDF 教程</a>
	<a href="../react">React 教程</a>
	<a href="../redis">Redis 教程</a>
	<a href="../rss">RSS 教程</a>
	<a href="../ruby">Ruby 教程</a>
	<a href="../rust">Rust 教程</a>
	<a href="../sass">Sass 教程</a>
	<a href="../scala">Scala 教程</a>
	<a href="../scipy">SciPy 教程</a>
	<a href="../servlet">Servlet 教程</a>
	<a href="../soap">SOAP 教程</a>
	<a href="../sql">SQL 教程</a>
	<a href="../sqlite">SQLite 教程</a>
	<a href="../svg">SVG 教程</a>
	<a href="../svn">SVN 教程</a>
	<a href="../swift">Swift 教程</a>
	<a href="../tcpip">TCP/IP 教程</a>
	<a href="../typescript">TypeScript 教程</a>
	<a href="../vbscript">VBScript 教程</a>
	<a href="../vue2">Vue.js 教程</a>
	<a href="../vue3">Vue3 教程</a>
	<a href="../w3c">W3C 教程</a>
	<a href="../webservices">Web Service 教程</a>
	<a href="../wsdl">WSDL 教程</a>
	<a href="../xlink">XLink 教程</a>
	<a href="../dom">XML DOM 教程</a>
	<a href="../schema">XML Schema 教程</a>
	<a href="../xml">XML 教程</a>
	<a href="../xpath">XPath 教程</a>
	<a href="../xquery">XQuery 教程</a>
	<a href="../xslfo">XSLFO 教程</a>
	<a href="../xsl">XSLT 教程</a>
	<a href="../data-structures">数据结构</a>
	<a href="../regexp">正则表达式</a>
	<a href="../quiz">测验</a>
	<a href="../browsers">浏览器</a>
	<a href="../quality">网站品质</a>
	<a href="../web">网站建设指南</a>
	<a href="../hosting">网站服务器教程</a>
	<a href="../design-pattern">设计模式</a>
			
		</div> 
		 	 </div>
</div>
	</div>
</div>


<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>